package no.ntnu.idi.deid.decision.validators;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import no.ntnu.idi.deid.documentunits.Sentence;
import no.ntnu.idi.deid.documentunits.Token;
import no.ntnu.idi.deid.searchresultunits.CategoryTag;
import no.ntnu.idi.deid.searchresultunits.Tag;

public class DoseMatcher {
	private static final String regexString = "((\\d+[(,\\d+)|\\.]?([-|x|+])*)+)|(x\\d+)";
	
	
	/**
	 * Searches the sentence for the pattern ((\\d+[(,\\d+)|\\.]?([-|x|+])*)+)|(x\\d+), and returns a list of matching
	 * subparts. If the pattern doesnt exists in the sentence, an empty list is returned.
	 * @param sentence The sentence to match
	 * @return A list containing the matched substrings
	 */
	public List<String> checkSentence(String sentence) {
		List<String> tokenList = new ArrayList<String>();
		Pattern p = Pattern.compile(regexString);
		Matcher m = p.matcher(sentence.toLowerCase());
		m.reset();
		while(m.find()) {
			List<String> list = addElementsToList(m.start(),m.end(),sentence);
			tokenList.addAll(list);
		}
		return tokenList;		
	}
	
	private List<String> addElementsToList(int startindex, int endindex, String str) {
		List<String> list = new ArrayList<String>();
		String[] temp = str.split(" ");
		int index = 0;
		int index2 = str.length();
		int tokenNumberStart = 0;
		int tokenNumberEnd = 0;
		for(int i = 0; i < temp.length; i++){
			index += temp[i].length()+1;
			if(startindex < index){
				tokenNumberStart = i;
				break;
			}
		}
		
		for(int i = temp.length-1; i >= 0; i--){
			index2 -= temp[i].length();
			index2--;
			if(endindex > index2){
				tokenNumberEnd = i;
				break;
			}
		}
		for(int i = tokenNumberStart; i <= tokenNumberEnd;i++){	
			list.add(temp[i]);
		}
		return list;
	}
		
	
	/**
	 * Searches the sentence for the pattern ((\\d+[(,\\d+)|\\.]?([-|x|+])*)+)|(x\\d+), and returns a list of matching
	 * subparts. If the pattern doesnt exists in the sentence, an empty list is returned.
	 * @param sentence The sentence to match
	 * @return A list containing the matched Tokens
	 */
	public List<Token> checkSentence(Sentence sentence) {
		List<String> tokenList = checkSentence(sentence.getOrginal().toLowerCase());
		List<Token> returnList = new ArrayList<Token>();
		for(String str : tokenList) {
			Token t = sentence.getTokenFromString(str);	
			if(t == null) {
				System.err.println("DoseMatcher.java: getTokenFromString(Sentence.java) could not find string(Should never happen)");
			}
			else {
				if(!t.hasSearchResult()) {
					t.addTag(Tag.DOSE_UNIT);
					returnList.add(t);
				}
			}
		}
		return returnList;
	}
	
	public static void createMedicamentPhrase(Sentence sentence) {
		
		int firstInstance = sentence.getEndIndex();
		int lastInstance = sentence.getStartIndex();
		for(Token t: sentence) {
			if(t.containsCategoryTag(Tag.MEDICAMENT)|| 
					t.containsCategoryTag(Tag.DOSE)||
					t.containsCategoryTag(Tag.DOSE_UNIT)) {
//				System.out.println(sentence.getStrippedSentence());
				
				if(t.getIndex() < firstInstance) {
					firstInstance = t.getIndex();
				}
				if(t.getIndex() > lastInstance) {
					lastInstance = t.getIndex();
				}
		   }
		}
		if(firstInstance < lastInstance) {
			sentence.addPhraseFromDocumentIndex(firstInstance, lastInstance, Tag.MEDICAMENT_PHRASE);
		}
	}
	
}
